﻿Date.prototype.toLocaleString = function () {
    function addZero(num) {
        if (num < 10)
            return "0" + num;
        return num;
    }
    return this.getFullYear() + "-" + addZero(this.getMonth() + 1) + "-" + addZero(this.getDate()) + " " + addZero(this.getHours()) + ":" + addZero(this.getMinutes()) + ":" + addZero(this.getSeconds());
};
function changeDateFormat(cellval) {
    var tempdate = new Date(cellval);
    return tempdate.toLocaleString();
};
function splitData(rawData) {
    console.log('rawData',rawData)
    var categoryData = [];
    var values = []
    for (var i = 0; i < rawData.length; i++) {
        categoryData.push(rawData[i].splice(0, 1)[0]);
        values.push(rawData[i])
    }
    return {
        categoryData: categoryData,
        values: values
    };
}

function calculateMA(dayCount,dataObj) {
    var result = [];
    //var data0 = splitData(arry);
    for (var i = 0, len = dataObj.values.length; i < len; i++) {
        if (i < dayCount) {
            result.push('-');
            continue;
        }
        var sum = 0;
        for (var j = 0; j < dayCount; j++) {
            sum += dataObj.values[i - j][1];
        }
        result.push(sum / dayCount);
    }
    return result;
}
//获取合约数据
function gethyhq(code) {
    $.getJSON('http://122.14.208.164:19000/index?m=test&a=get_real', { symbol: code },
        function (data) {
            var data2 = data.data
                .replace(/'/g, '"')
                .replace(/nan/g, '"nan"')
                .replace(/False/g, 'false')
                .replace(/True/g, 'true');
            var json = eval('(' + data2 + ')');
            //console.log('gethyhq json', json)
            $(".sell_price").html(json.ask_price1);
            $(".sell_num").html(json.ask_volume1);
            $(".buy_price").html(json.bid_price1);
            $(".buy_num").html(json.bid_volume1);
            $(".hold").html(json.open_interest);
        });

}
//获取K线数据
function MakeKLine(code) {
    var arry2 = [];
    $.post('/Mobile/MIndex/get_kline', { code: code }, function (data) {
        var KLineData = eval('(' + data + ')');
        console.log('KLineData', KLineData)
        var datetime = KLineData.data.datetime;
        var open = KLineData.data.open;
        var close = KLineData.data.close;
        var high = KLineData.data.high;
        var low = KLineData.data.low;
        var arry1 = [];
        $.each(datetime, function (index, item) {
            var stamptime = datetime[index]
            //debugger;
            var datestr = changeDateFormat(Math.round(stamptime / 1000000) + index * 0);
            //console.log('datastr', datestr)
            //console.log('datetime', datestr)
            arry1.push(datestr);
            arry1.push(open[index]);
            arry1.push(close[index]);
            arry1.push(low[index]);
            arry1.push(high[index]);

            arry2.push(arry1)
            arry1 = [];
        });
        SetKline(code,arry2);


    })

    //setTimeout(function () { MakeKLine(code) },5000)
    return arry2;
}

function SetKline(code,arry) {
    var upColor = '#ec0000';
    var upBorderColor = '#8A0000';
    var downColor = '#00da3c';
    var downBorderColor = '#008F28';
    var dom = document.getElementById("container");
    var myChart = echarts.init(dom);
    var dataObj = splitData(arry);
    var option;
   
    $.post('/Mobile/MIndex/GetMyHyByCode', { code: code }, function (data) {
        var pointjson = eval('(' + data + ')');
        //console.log('GetMyHyByCode', pointjson);
        var pointdata = [
            //{
            //    name: 'XX标点',
            //    coord: ['2021-02-19 21:44:00', 9188],
            //    value: 9188,
            //    itemStyle: {
            //        color: '#000099'
            //    }
            //},
            {
                name: 'highest value',
                type: 'max',
                valueDim: 'highest',
                //itemStyle: {
                //    color: '#008F28'
                //}
            },
            {
                name: 'lowest value',
                type: 'min',
                valueDim: 'lowest'
            },
            {
                name: 'average value on close',
                type: 'average',
                valueDim: 'close'
            }
        ];
        $.each(pointjson, function (index, item) {

            var point =
            {
                name: item.F_Id,
                coord: [item.tradingday, item.currentprice],
                value: '',
                itemStyle: {
                    color: setColor(item.direction, item.openclose)
                },
                label: {
                    show: true,
                    position: 'inside',
                    formatter: item.openclose + item.direction,
                    //color:'#000'
                }
            };
            pointdata.push(point);

            //pointdata.push({
            //    name: item.F_Id,
            //    coord: [item.tradingday, item.currentprice],
            //    value: item.currentprice,
            //    itemStyle: {
            //        color: setColor(item.direction, item.openclose)
            //    }
            //});
        });
        /////////////////////////////////////////////////////////////////

        option = {
            title: {
                text: '',
                left: 0
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross'
                }
            },
            legend: {
                data: ['实时', '5分钟', '10分钟', '20分钟', '30分钟']
            },
            grid: {
                left: '10%',
                right: '10%',
                bottom: '1%'
            },
            xAxis: {
                type: 'category',
                data: dataObj.categoryData,
                scale: true,
                boundaryGap: false,
                axisLine: { onZero: false },
                splitLine: { show: false },
                splitNumber: 20,
                min: 'dataMin',
                max: 'dataMax'
            },
            yAxis: {
                scale: true,
                splitArea: {
                    show: true
                }
            },
            dataZoom: [
                {
                    type: 'inside',
                    start: 0,
                    end: 100
                },
                {
                    show: true,
                    type: 'slider',
                    top: '90%',
                    start: 0,
                    end: 100
                }
            ],
            series: [
                {
                    name: '实时',
                    type: 'candlestick',
                    data: dataObj.values,
                    itemStyle: {
                        color: upColor,
                        color0: downColor,
                        borderColor: upBorderColor,
                        borderColor0: downBorderColor
                    },
                    markPoint: {
                        label: {
                            normal: {
                                formatter: function (param) {
                                    //console.log('markpoint normal param', param);
                                    return param != null ? Math.round(param.value) : '';
                                }
                            }
                        },
                        data: pointdata,
                        tooltip: {
                            formatter: function (param) {
                                //console.log('markpoint tooltip param', param);
                                return param.name + '<br>' + (param.data.coord || '');
                            }
                        }
                    },
                    markLine: {
                        symbol: ['none', 'none'],
                        data: [
                            [
                                {
                                    name: 'from lowest to highest',
                                    type: 'min',
                                    valueDim: 'lowest',
                                    symbol: 'circle',
                                    symbolSize: 10,
                                    label: {
                                        show: false
                                    },
                                    emphasis: {
                                        label: {
                                            show: false
                                        }
                                    }
                                },
                                {
                                    type: 'max',
                                    valueDim: 'highest',
                                    symbol: 'circle',
                                    symbolSize: 10,
                                    label: {
                                        show: false
                                    },
                                    emphasis: {
                                        label: {
                                            show: false
                                        }
                                    }
                                }
                            ],
                            {
                                name: 'min line on close',
                                type: 'min',
                                valueDim: 'close'
                            },
                            {
                                name: 'max line on close',
                                type: 'max',
                                valueDim: 'close'
                            }
                        ]
                    }
                },
                {
                    name: '5分钟',
                    type: 'line',
                    data: calculateMA(5, dataObj),
                    smooth: true,
                    lineStyle: {
                        opacity: 0.5
                    }
                },
                {
                    name: '10分钟',
                    type: 'line',
                    data: calculateMA(10, dataObj),
                    smooth: true,
                    lineStyle: {
                        opacity: 0.5
                    }
                },
                {
                    name: '20分钟',
                    type: 'line',
                    data: calculateMA(20, dataObj),
                    smooth: true,
                    lineStyle: {
                        opacity: 0.5
                    }
                },
                {
                    name: '30分钟',
                    type: 'line',
                    data: calculateMA(30, dataObj),
                    smooth: true,
                    lineStyle: {
                        opacity: 0.5
                    }
                },

            ]
        };


        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }





    });



}

function setColor(direction, openclose) {
    if (direction == '买' && openclose == '开仓')
        return '#FF0000';//红
    else
    if (direction == '买' && openclose == '平仓')
        return '#6600FF';//紫
    else
    if (direction == '卖' && openclose == '开仓')
        return '#FF7F24';//橙
    else
    if (direction == '卖' && openclose == '平仓')
        return '#9AFF9A';//浅绿
    else
        return '#FF0000';


}


 